package com.sport.service.impl;

import com.sport.bean.Kit;
import com.sport.bean.PageParam;
import com.sport.bean.Rent;
import com.sport.dao.IKitDao;
import com.sport.dao.IRentDao;
import com.sport.service.IKitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author wxq
 * @Date 2021/5/9
 **/
@Service
public class KitServiceImpl implements IKitService {

    @Autowired
    private IKitDao kitDao;

    @Autowired
    private IRentDao rentDao;

    @Override
    @Transactional
    public int rentKit(Rent rent) throws Exception {
        //根据kitId查找对应器材信息
        Kit kit = findById(rent.getKitId());
        //计算剩余数量
        kit.setLeftNum(kit.getAmount() - kit.getRentNum());
        //借用数量是否大于剩余数量
        if (rent.getNum() > kit.getLeftNum()) {
            throw new Exception("库存数量不足");
        }
        //更新库存数量
        kit.setRentNum(kit.getRentNum() + rent.getNum());
        kitDao.updateKit(kit);
        //添加到借用者信息
        int i = rentDao.addRent(rent);
        return i;
    }

    @Override
    public int addKit(Kit kit) {
        return kitDao.addKit(kit);
    }

    @Override
    public int deletedKit(Integer kId) {
        return kitDao.deletedKit(kId);
    }

    @Override
    public List<Kit> allKits() {
        return kitDao.allKits();
    }

    @Override
    public Kit findById(String kId) {
        return kitDao.findById(kId);
    }

    @Override
    public Kit findOneById(Integer Id) {
        return kitDao.findOneById(Id);
    }

    @Override
    public int updateKit(Kit kit) {
        return kitDao.updateKit(kit);
    }

//    分页
    @Override
    public List<Kit> listPage(PageParam<Kit> pageParam) {
        return kitDao.listPage(pageParam);
    }
}
